#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>
#include <QtDebug>
#include <windows.h>
#include <QWindow>


struct MainWindowPrivate
{
    MainWindowPrivate()
    {

    }

    void deleteProcess()
    {
        if (p1) p1->kill();
        if (p2) p2->kill();
        if (p3) p3->kill();
        if (p4) p4->kill();
    }

    QProcess * p1;
    QProcess * p2;
    QProcess * p3;
    QProcess * p4;
};
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    _p->p1 = new QProcess(this);
    _p->p2 = new QProcess(this);
    _p->p3 = new QProcess(this);
    _p->p4 = new QProcess(this);
    connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(TabChange(int)));
}

MainWindow::~MainWindow()
{
    delete ui;
    _p->deleteProcess();
    delete _p;
}

void MainWindow::TabChange(int index)
{
    if(index == 1)
    {
        qDebug()<< "over 0";
        QString cmd = QString::fromLocal8Bit("C:/Windows/System32/notepad.exe");
        QStringList arguments;
        RunCmd(cmd,arguments);
    }
    else if(index == 1)
    {
        qDebug()<< "over 0";
        QString cmd = QString::fromLocal8Bit("C:/Windows/System32/notepad.exe");
        QStringList arguments;
        RunCmd(cmd,arguments);
    }
    else if(index == 1)
    {
        qDebug()<< "over 0";
        QString cmd = QString::fromLocal8Bit("C:/Windows/System32/notepad.exe");
        QStringList arguments;
        RunCmd(cmd,arguments);
    }
    else if(index == 1)
    {
        qDebug()<< "over 0";
        QString cmd = QString::fromLocal8Bit("C:/Windows/System32/notepad.exe");
        QStringList arguments;
        RunCmd(cmd,arguments);
    }
    else
    {
        qDebug()<< "over big";
    }
}

void MainWindow::RunCmd(QString exePath, QStringList argv)
{
    _p->p1->start(exePath,argv);
    _p->p1->waitForFinished(2000);
    WId wid = (WId)FindWindow(QStringLiteral("Notepad").toStdWString().c_str()
            , QStringLiteral("无标题 - 记事本").toStdWString().c_str());
    QWindow * window = QWindow::fromWinId(wid);
    if (window)
        {
            window->setFlags(window->flags() | Qt::CustomizeWindowHint | Qt::WindowTitleHint); //这边可以设置一下属性

            QWidget * widget = QWidget::createWindowContainer(window, this, Qt::Widget);

            ui->tab = widget;
        }
}
